package InterviewTest.q0103_replaceSpaces;

public class Solution_2 {
    public String replaceSpaces(String S, int length) {
        // 边界条件
        if(S == null || S.length() == 0) {
            return S;
        }

        char [] str = S.toCharArray();
        int index = str.length - 1;
        for(int i=length - 1; i >= 0; i--) {
            if(str[i] == ' ') {
                str[index--] = '0';
                str[index--] = '2';
                str[index--] = '%';
            }else{
                str[index--] = str[i];
            }
        }
        return new String(str, index + 1, str.length - index - 1);
    }

}
